// xc_lshop/shop/coupon/received.js
var actcouponbill = require('../../../xc/model/coupon_cont')
var html2wxml = require('../../../html2wxml/html2wxml.js');

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
     code:""
  },
  xcloaddata: function (options, config) {
    var couponid=options.couponid
    var updateconfig={}
    if (config.prothem && config.prothem.themcolor) {
      var themconfig = config.prothem.themcolor.config;
      updateconfig['themcolor'] = themconfig.themcolor
    }
    console.log("updateconfig")
    console.log(updateconfig)
    this.setData(updateconfig);
    this.getcoupon(couponid);
  },
  getcoupon(couponid){
    var that=this;
    actcouponbill.getcoupon(couponid).then(res=>{
      var updatemodel={}
      console.log(res['obj'])

      if(res["status"]==1){ 
        html2wxml.html2wxml('explain', res['obj']['remark'], this, 0);
        updatemodel['xcmodel']=res['obj']
        that.setData(updatemodel)
      }
      else
      {
       var message="优惠券不存在";
        if(res["message"]){
          message=res["message"]
        }
        that.error(message, true)
      }

    })
  },
  // 领取优惠券
  receivecoupon(ev){
  
    var item= this.data.xcmodel;
    var that = this
    var app =getApp();

    app.xc_sys_config.authop.oppower("login").then(function (res) {
     
      actcouponbill.receivecoupon(item.id,that.data.code).then(res=>{
        var updatemodel={}
        var keyval="xcmodel."
        if(res["status"]==1){     
           updatemodel[keyval+"myreceivednum"]=res["obj"].myreceivednum
           updatemodel[keyval+"receivednum"]=res["obj"].receivednum
           
           
          that.setData(updatemodel)
         
          wx.showToast({
            title: '领取成功',
          })
        }
        else
        {
          if( res["status"]==-1){
            wx.showToast({
              title: res["message"],
            })
          }else if ( res["status"]==2){
            updatemodel[keyval+"receivednum"]=this.data.xcmodel.num;
            that.setData(updatemodel)
          }
          else if ( res["status"]==3){
            updatemodel[keyval+"myreceivednum"]=this.data.xcmodel.everyonelimit
            that.setData(updatemodel)
          }
  
        }
  
      })


    })
    
   
   


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})